public class SingleLinkedList implements List{

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head = null;
    public ListNode tail = null;

    public int usedSize = 0;

    // 1、无头单向非循环链表实现
        //头插法
        public void addFirst(int data) {
            ListNode node = new ListNode(data);
            if(isEmpty()) {
                head = tail = node;
            }else {
                node.next = head;
                head = node;
            }
            usedSize++;
        }
        //尾插法
        public void addLast(int data) {
            ListNode node = new ListNode(data);
            if(isEmpty()) {
                head = tail = node;
            } else {
                tail.next = node;
                tail = tail.next;
            }
            usedSize++;
        }
        //任意位置插入,第一个数据节点为0号下标
        public boolean addIndex(int index,int data) {
            if(index < 0||index > usedSize) return false;
            ListNode node = new ListNode(data);
            if(isEmpty()) {
                head = tail = node;
            }
            if(index == 0) {
                node.next = head;
                head = node;
            }else if(index == usedSize) {
                tail.next = node;
                tail = tail.next;
            }else {
                int ret = index - 1;
                ListNode cur = head;
                while (ret != 0) {
                    cur = cur.next;
                    ret--;
                }
                node.next = cur.next;
                cur.next = node;
            }
            usedSize++;
            return true;
        }
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key) {
            if(isEmpty()) {
                return false;
            }
            ListNode cur = head;
            while(cur != null) {
                if(cur.val == key) {
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }
        //删除第一次出现关键字为key的节点
        public void remove(int key) {
            if(isEmpty()) {
                return;
            }
            if(head.val == key) {
                head = head.next;
                usedSize--;
                return;
            }
            ListNode prev = head;
            ListNode cur = head.next;
            while(cur != null) {
                if(cur.val == key) {
                    prev.next = cur.next;
                    usedSize--;
                    return;
                }
                prev = cur;
                cur = cur.next;
            }
        }
        //删除所有值为key的节点
        public void removeAllKey(int key) {
            if(isEmpty()) {
                return;
            }
            ListNode prev = head;
            ListNode cur = head.next;
            while(cur != null) {
                if(cur.val == key) {
                    prev.next = cur.next;
                    cur = cur.next;
                    usedSize--;
                }else {
                    prev = prev.next;
                    cur = cur.next;
                }
            }
            if(head.val == key) {
                head = head.next;
                usedSize--;
            }
        }
        //得到单链表的长度
        public int size() {
        return usedSize;
        }
        public void display() {
            ListNode cur = head;
            while(cur != null) {
                System.out.print(cur.val + " ");
                cur = cur.next;
            }
        }
        public void clear() {

//            当链表是纯内部使用的数据结构时
//            确保没有外部代码持有节点引用
//            追求极致性能的场合


//           head = null;
//           usedSize = 0;


//            公开的 API 或类库代码
//            无法确保外部是否持有节点引用
//            对内存安全要求极高的系统
            ListNode cur = head;
            while (cur != null) {
                ListNode curN = cur.next;
                cur.next = null;
                cur = curN;
            }
            head = null;
            usedSize = 0;
        }

        public boolean isEmpty() {
            return head == null;
        }

}
